package cn.gmph.blog.admin.service.sys.dao.helper;

import org.apache.ibatis.jdbc.SQL;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.gmph.blog.admin.service.sys.entity.UsersDO;
import cn.gmph.core.utils.StringUtils;
public class UsersProvider {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String TABLE_NAME = "wp_users";

    public String update(Map<String, Object> params) {
        UsersDO users = (UsersDO) params.get("users");
        return new SQL() {{
        UPDATE(TABLE_NAME);
        if (StringUtils.isNotBlank(users.getUserLogin())){
            SET("user_login=#{users.userLogin}");
        }
        if (StringUtils.isNotBlank(users.getUserPass())){
            SET("user_pass=#{users.userPass}");
        }
        if (StringUtils.isNotBlank(users.getUserNicename())){
            SET("user_nicename=#{users.userNicename}");
        }
        if (StringUtils.isNotBlank(users.getUserEmail())){
            SET("user_email=#{users.userEmail}");
        }
        if (StringUtils.isNotBlank(users.getUserUrl())){
            SET("user_url=#{users.userUrl}");
        }
        if (users.getUserRegistered() != null) {
            SET("user_registered=#{users.userRegistered}");
        }
        if (StringUtils.isNotBlank(users.getUserActivationKey())){
            SET("user_activation_key=#{users.userActivationKey}");
        }
        if (users.getUserStatus() != null) {
            SET("user_status=#{users.userStatus}");
        }
        if (StringUtils.isNotBlank(users.getDisplayName())){
            SET("display_name=#{users.displayName}");
        }
        WHERE("id = #{users.id}");
        }}.toString();
    }

    public String pageList(Map<String, Object> params) {
        UsersDO users = (UsersDO) params.get("users");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {{
        SELECT("*");
        FROM(TABLE_NAME);
        if (users.getId() != null) {
            WHERE("ID=#{users.id}");
        }
        if (StringUtils.isNotBlank(users.getUserLogin())){
            WHERE("user_login=#{users.userLogin}");
        }
        if (StringUtils.isNotBlank(users.getUserPass())){
            WHERE("user_pass=#{users.userPass}");
        }
        if (StringUtils.isNotBlank(users.getUserNicename())){
            WHERE("user_nicename=#{users.userNicename}");
        }
        if (StringUtils.isNotBlank(users.getUserEmail())){
            WHERE("user_email=#{users.userEmail}");
        }
        if (StringUtils.isNotBlank(users.getUserUrl())){
            WHERE("user_url=#{users.userUrl}");
        }
        if (users.getUserRegistered() != null) {
            WHERE("user_registered=#{users.userRegistered}");
        }
        if (StringUtils.isNotBlank(users.getUserActivationKey())){
            WHERE("user_activation_key=#{users.userActivationKey}");
        }
        if (users.getUserStatus() != null) {
            WHERE("user_status=#{users.userStatus}");
        }
        if (StringUtils.isNotBlank(users.getDisplayName())){
            WHERE("display_name=#{users.displayName}");
        }
        ORDER_BY("id desc limit " + start + ", " + limit );
        }}.toString();
    }

    public String pageListCount(Map<String, Object> params) {
        UsersDO users = (UsersDO) params.get("users");
        return new SQL() {{
        SELECT("count(1)");
        FROM(TABLE_NAME);
        if (users.getId() != null) {
            WHERE("ID=#{users.id}");
        }
        if (StringUtils.isNotBlank(users.getUserLogin())){
            WHERE("user_login=#{users.userLogin}");
        }
        if (StringUtils.isNotBlank(users.getUserPass())){
            WHERE("user_pass=#{users.userPass}");
        }
        if (StringUtils.isNotBlank(users.getUserNicename())){
            WHERE("user_nicename=#{users.userNicename}");
        }
        if (StringUtils.isNotBlank(users.getUserEmail())){
            WHERE("user_email=#{users.userEmail}");
        }
        if (StringUtils.isNotBlank(users.getUserUrl())){
            WHERE("user_url=#{users.userUrl}");
        }
        if (users.getUserRegistered() != null) {
            WHERE("user_registered=#{users.userRegistered}");
        }
        if (StringUtils.isNotBlank(users.getUserActivationKey())){
            WHERE("user_activation_key=#{users.userActivationKey}");
        }
        if (users.getUserStatus() != null) {
            WHERE("user_status=#{users.userStatus}");
        }
        if (StringUtils.isNotBlank(users.getDisplayName())){
            WHERE("display_name=#{users.displayName}");
        }
        }}.toString();
    }
}

